﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Data.Entity;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;
using EKAABSInformationManagement.Service;
using EKAABSInformationManagement.Models;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Controllers
{
    public class RequestManageController : Controller
    {
        //
        // GET: /RequestManage/

        public RequestManage RequestManage { get; private set; }
        public string from { get; private set; }
        public IAsyncSendingmail asyncSendmail { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (System.Web.HttpContext.Current.User.IsInRole("admin") != true)
            {
                HttpException ex = new HttpException(401, "");
                requestContext.HttpContext.AddError(ex);
            }
            this.from = string.IsNullOrEmpty(ConfigurationManager.AppSettings["userName"]) ? "" : ConfigurationManager.AppSettings["userName"];
            this.from = this.from.Contains("@wicresoft.com") ? this.from : string.Concat(this.from, "@wicresoft.com");
            if (RequestManage == null) { RequestManage = new RequestManage(); }
            if (asyncSendmail == null) { asyncSendmail = new asyncSendingmail(); }
            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            string status = Request.QueryString["status"];
            int s = default(int);
            Int32.TryParse(status, out s);
            List<requirementRecords> ReqMag = RequestManage.getRecordsBystatus(s);
            return View(ReqMag);
        }

        [HttpPost]
        public ActionResult Receive(string ids, requirementRecords a)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                List<requirementRecords> records = RequestManage.Receive(strs, a);
                asyncSendmail.ccRecipient = this.from;
                asyncSendmail.mailSubject = "你的物品申请已经进入审批";
                string user_c = string.Empty;
                foreach (requirementRecords r in records)
                {
                    if (!string.IsNullOrEmpty(user_c) && user_c != r.employee)
                    {
                        user_c = r.employee;
                        asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                        asyncSendmail.AsyncSendmail();
                        asyncSendmail.recipient = Membership.GetUser(user_c).Email;
                        asyncSendmail.mailBody = string.Concat("Hi ", user_c, ",\r\n\r\n你的下列物品申请已经进入审批，详情如下：\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n管理员备注：", r.comments);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(user_c))
                        {
                            user_c = r.employee;
                            asyncSendmail.recipient = Membership.GetUser(user_c).Email;
                            asyncSendmail.mailBody = string.Concat("Hi ", user_c, ",\r\n\r\n你的下列物品申请已经进入审批，详情如下：\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n管理员备注：", r.comments);
                        }
                        else
                        {
                            asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n管理员备注：", r.comments);
                        }
                    }
                }
                asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                asyncSendmail.AsyncSendmail();
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult Pass(string ids, requirementRecords a)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                List<requirementRecords> records = RequestManage.Pass(strs, a);
                asyncSendmail.ccRecipient = this.from;
                asyncSendmail.mailSubject = "你的物品申请已经通过审批";
                string user_c = string.Empty;
                foreach (requirementRecords r in records)
                {
                    if (!string.IsNullOrEmpty(user_c) && user_c != r.employee)
                    {
                        user_c = r.employee;
                        asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                        asyncSendmail.AsyncSendmail();
                        asyncSendmail.recipient = Membership.GetUser(user_c).Email;
                        asyncSendmail.mailBody = string.Concat("Hi ", user_c, ",\r\n\r\n你的下列物品申请已经通过审批，详情如下：\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n管理员备注：", r.comments);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(user_c))
                        {
                            user_c = r.employee;
                            asyncSendmail.recipient = Membership.GetUser(user_c).Email;
                            asyncSendmail.mailBody = string.Concat("Hi ", user_c, ",\r\n\r\n你的下列物品申请已经通过审批，详情如下：\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n管理员备注：", r.comments);
                        }
                        else
                        {
                            asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n管理员备注：", r.comments);
                        }
                    }
                }
                asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                asyncSendmail.AsyncSendmail();
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult Reject(string ids, requirementRecords a)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                List<requirementRecords> records = RequestManage.Reject(strs, a);
                asyncSendmail.ccRecipient = this.from;
                asyncSendmail.mailSubject = "你的物品申请被驳回";
                string user_c = string.Empty;
                foreach (requirementRecords r in records)
                {
                    if (!string.IsNullOrEmpty(user_c) && user_c != r.employee)
                    {
                        user_c = r.employee;
                        asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                        asyncSendmail.AsyncSendmail();
                        asyncSendmail.recipient = Membership.GetUser(user_c).Email;
                        asyncSendmail.mailBody = string.Concat("Hi ", user_c, ",\r\n\r\n你的下列物品申请被驳回，详情如下：\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n驳回原因：", r.comments);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(user_c))
                        {
                            user_c = r.employee;
                            asyncSendmail.recipient = Membership.GetUser(user_c).Email;
                            asyncSendmail.mailBody = string.Concat("Hi ", user_c, ",\r\n\r\n你的下列物品申请被驳回，详情如下：\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n驳回原因：", r.comments);
                        }
                        else
                        {
                            asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description, "\r\n驳回原因：", r.comments);
                        }
                    }
                }
                asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                asyncSendmail.AsyncSendmail();
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }

    }
}
